package com.example.petshop.service.impl;

import com.example.petshop.entity.Goods;
import com.example.petshop.entity.Order;
import com.example.petshop.mapper.GoodsMapper;
import com.example.petshop.mapper.OrderMapper;
import com.example.petshop.service.GoodsService;
import com.example.petshop.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private GoodsService goodsService;
    @Override
    public void saveOrder(Order order) {
        // 插入订单到数据库
        orderMapper.insert(order);
    }

    @Override
    public List<Order> getAll() {
        return orderMapper.getAll();
    }

    @Override
    public List<Order> getOrdersByIds(List<Integer> ids) {
        return orderMapper.selectOrdersByIds(ids);
    }

    @Override
    public void updateOrderState(List<Integer> orderIds, String newState) {
        System.out.println("OrderServiceImpl OrderIds: "+orderIds +"newState: "+newState);
        if (newState == null) {
            throw new IllegalArgumentException("New state cannot be null");
        }
        for (Integer orderId : orderIds) {
            try {
                System.out.println("Updating order ID: " + orderId + " to state: " + newState);
                Order order = orderMapper.findById(orderId);
                if (order == null) {
                    System.out.println("Order not found for ID: " + orderId);
                    continue; // Skip to next order
                }
                order.setOrderState(newState);
                int rowsAffected = orderMapper.updateById(order);
                System.out.println("Rows affected for ID " + orderId + ": " + rowsAffected);
            } catch (Exception e) {
                System.err.println("Error updating order ID " + orderId + ": " + e.getMessage());
            }
        }
    }

    @Override
    public List<Order> getByUserIdAndState(Long userId, String state) {
        return orderMapper.findByUserIdAndState(userId, state);
    }

    @Override
    public void updateOrder(Order order) {
        orderMapper.updateOrder(order);
    }

    @Override
    public void updateRemark(int orderId, String remark) {
        Order order = orderMapper.findById(orderId);
        order.setRemark(remark);
        orderMapper.saveReamark(order);
    }

    @Override
    public void updateReason(int orderId, String reason) {
        Order order = orderMapper.findById(orderId);
        order.setReason(reason);
        orderMapper.saveReason(order);
    }

    @Override
    public List<Order> getAfterOrdersByUserIdAndStates(Long userId, List<String> states) {
       return orderMapper.findByUserIdAndOrderStateIn(userId, states);
    }

    @Override
    public List<Order> getRemarksByGoodsName(String goodsName) {
        return orderMapper.findRemarksByGoodsName(goodsName);
    }
    @Override
    public void updateGoodsInventoryAndSales(List<Integer> orderIds) {
        List<Order> orders = getOrdersByIds(orderIds);

        for (Order order : orders) {
            Goods goods = goodsService.getGoodsByName(order.getOrderName());
            System.out.println("Goods before update: " + goods);

            if (goods != null) {
                int orderCount = order.getOrderCount() != null ? Integer.parseInt(order.getOrderCount()) : 0;

                // 更新库存
                int currentCount = goods.getGoodsCount() != null ? Integer.parseInt(goods.getGoodsCount()) : 0;
                goods.setGoodsCount(String.valueOf(currentCount - orderCount));

                // 更新整体销量
                int currentSales = goods.getSales() != null ? Integer.parseInt(goods.getSales()) : 0;
                goods.setSales(String.valueOf(currentSales + orderCount));

                // 更新周销量
                int currentWeekSales = goods.getWeekSales() != null ? Integer.parseInt(goods.getWeekSales()) : 0;
                System.out.println("Current Week Sales: " + currentWeekSales + ", New Week Sales: " + (currentWeekSales + orderCount));
                goods.setWeekSales(String.valueOf(currentWeekSales + orderCount));

                // 更新月销量
                int currentMonthSales = goods.getMonthSales() != null ? Integer.parseInt(goods.getMonthSales()) : 0;
                System.out.println("Current Month Sales: " + currentMonthSales + ", New Month Sales: " + (currentMonthSales + orderCount));
                goods.setMonthSales(String.valueOf(currentMonthSales + orderCount));

                // 更新年销量
                int currentYearSales = goods.getYearSales() != null ? Integer.parseInt(goods.getYearSales()) : 0;
                System.out.println("Current Year Sales: " + currentYearSales + ", New Year Sales: " + (currentYearSales + orderCount));
                goods.setYearSales(String.valueOf(currentYearSales + orderCount));

                System.out.println("Goods after update: " + goods);
                goodsService.updateGoods(goods);
                System.out.println("Goods updated successfully");
            } else {
                throw new RuntimeException("Goods not found for order: " + order.getOrderName());
            }
        }
    }

    @Override
    public List<Order> getByShopId(int shopId) {
        return orderMapper.findByShopId(shopId);
    }

//    @Override
//    public List<Order> getByState(String state) {
//        return orderMapper.getByState(state);
//    }


    // 可以添加其他业务逻辑方法，例如获取订单、更新订单状态等
}